<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\InviteOneOnOneStudent;
use Illuminate\Support\Facades\DB;

class InviteOneOnOneStudentStatusOnCommand extends Command
{
    protected $signature = 'bills:invite_one_on_one_student_status_on';

    public function handle()
    {
        $pageSize = 1000;

        $productIds = [1057,1058,1061];

        $BillingQuery = DB::connection('billing')->table('student_products')
            ->where('status', 0)//排除退款
            ->whereIn('product_id', $productIds);

        $billTotalNum = $BillingQuery->count();

        for ($page=1; $page<=ceil($billTotalNum/$pageSize); $page++) {
            $studentProducts = $BillingQuery->select(['student_id', 'status', 'bill_id', 'unit_price', 'lesson_count_used', ])
                ->skip($pageSize * ($page - 1))->take($pageSize)->get();

            foreach ($studentProducts as $studentProduct) {
                //
                if ($studentProduct->bill_id == 0) {
                    continue;
                }
                //单价大于0
                if ($studentProduct->unit_price <= 0) {
                    continue;
                }
                //必须有使用过
                if ($studentProduct->lesson_count_used == 0) {
                    continue;
                }

                $this->studentStatusOn($studentProduct->student_id);
            }
        }
    }

    private function studentStatusOn($studentId)
    {
        InviteOneOnOneStudent::setInviteStatus($studentId, 1, 1);
    }

}
